#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDesktopWidget>
#include <QGraphicsDropShadowEffect>
#include <QWebFrame>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //将主窗口指针传到控件，便于控制
    ui->webView->setMainWindow(this);

    this->setWindowTitle("Loy - Browser");

    this->centralWidget()->setMaximumSize(QApplication::desktop()->availableGeometry().size());

    //Qt::SplashScreen可以让程序不在任务栏显示图标
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    this->anim = new QPropertyAnimation(this, "windowOpacity", this);
    effect(0, 1, 300);

    this->setShadow(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setShadow(bool flag)
{
    if(flag == true) {
        QGraphicsDropShadowEffect* be = new QGraphicsDropShadowEffect(this);
        be->setOffset(0);
        be->setBlurRadius(10);
        be->setColor(QColor(0,0,0,255));
        this->ui->webView->setGraphicsEffect(be);
    } else {
        this->ui->webView->setGraphicsEffect(0);
    }
}

void MainWindow::effect(float startValue, float endValue, int time)
{
    anim->stop();
    anim->setDuration(time);
    anim->setStartValue(startValue);
    anim->setEndValue(endValue);
    anim->start();
}

void MainWindow::showNormal()
{
    this->centralWidget()->layout()->setMargin(10);
    QMainWindow::showNormal();
}

void MainWindow::showMaximized()
{
    this->centralWidget()->layout()->setMargin(0);
    QMainWindow::showMaximized();
}

void MainWindow::closeMainWindow()
{
    QMainWindow::close();
}

void MainWindow::close()
{
    connect(this->anim, SIGNAL(finished()), this, SLOT(closeMainWindow()));
    effect(1, 0, 300);
}
